package com.hspedu.newData.dmsxl.单调栈;

import org.junit.Test;

import java.util.HashMap;
import java.util.Stack;

/**
 * @Author: yh
 * @description: 
 * @CreateTime: 2025-04-26 
 * @Version: 1.0
 */

public class LC496下一个更大元素Ⅰ {

    // 单调栈
    public int[] nextGreaterElement(int[] nums1, int[] nums2) {

        HashMap<Integer, Integer> hashMap = new HashMap<>();

        Stack<Integer> stack = new Stack<>();
        for (int i = 0; i < nums2.length; i++) {
            while(!stack.isEmpty()&&nums2[i] > stack.peek()){
                int result = stack.pop();
                hashMap.put(result,nums2[i]);
            }
            stack.push(nums2[i]);
        }

        int[] res = new int[nums1.length];

        for (int i = 0; i < nums1.length; i++) {
            if (!hashMap.containsKey(nums1[i])){
                res[i] = -1;
                continue;
            }
            res[i] = hashMap.get(nums1[i]);
        }
        return res;
    }

    @Test
    public void test() {
        // 测试代码
        nextGreaterElement(new int[]{4,1,2},new int[]{1,3,4,2});
    }
}
